<template>
  <el-row :gutter="20" class="article-detail-container">
    <!-- 主内容区域 -->
    <el-col :span="24" :lg="18" :xl="16" :offset="0" :lg-offset="3" :xl-offset="4">
      <!-- 加载状态 -->
      <div class="loading-state" v-if="loading">
        <el-spinner size="large" />
        <p>加载中...</p>
      </div>
      
      <!-- 错误提示 -->
      <div class="error-state" v-else-if="error">
        <el-alert
          title="加载失败"
          :description="error"
          type="error"
          show-icon
        />
      </div>
      
      <!-- 文章内容 -->
      <div class="article-content" v-else-if="article">
        <div class="article-header">
          <h1 class="article-title">{{ article.title }}</h1>
          <div class="article-meta">
            <span class="publish-time">发布时间：{{ formatDate(article.createTime) }}</span>
            <span class="update-time" v-if="article.updateTime">更新时间：{{ formatDate(article.updateTime) }}</span>
            <span class="category">分类：{{ article.category || '未分类' }}</span>
            <span class="read-count">阅读 {{ article.readCount || 0 }}</span>
          </div>
        </div>
        
        <div class="article-body" v-html="article.content"></div>
        
        <div class="article-actions">
          <router-link to="/article" class="back-link">
            <el-button size="small">返回列表</el-button>
          </router-link>
          <router-link :to="'/admin/edit/' + article.id" class="edit-link">
            <el-button type="primary" size="small">编辑文章</el-button>
          </router-link>
        </div>
      </div>
      
      <!-- 文章不存在 -->
      <div class="not-found" v-else>
        <el-empty description="文章不存在或已被删除" />
        <router-link to="/article" class="back-home">
          <el-button type="primary">返回文章列表</el-button>
        </router-link>
      </div>
    </el-col>
    
    <!-- 侧边栏（大屏幕显示） -->
    <el-col :span="24" :lg="6" class="sidebar" v-if="!loading && !error && article">
      <div class="sidebar-card">
        <h3>相关信息</h3>
        <div class="info-item">
          <span class="label">发布时间：</span>
          <span class="value">{{ formatDate(article.createTime) }}</span>
        </div>
        <div class="info-item" v-if="article.updateTime">
          <span class="label">更新时间：</span>
          <span class="value">{{ formatDate(article.updateTime) }}</span>
        </div>
        <div class="info-item">
          <span class="label">分类：</span>
          <span class="value">{{ article.category || '未分类' }}</span>
        </div>
        <div class="info-item">
          <span class="label">阅读次数：</span>
          <span class="value">{{ article.readCount || 0 }}</span>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useArticleStore } from '../../store/articleStore'

export default {
  name: 'ArticleDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const articleStore = useArticleStore()
    const article = ref(null)
    const loading = ref(false)
    const error = ref(null)

    // 获取文章详情
    const fetchArticleDetail = async () => {
      const id = route.params.id
      if (!id) {
        error.value = '文章ID无效'
        return
      }
      
      loading.value = true
      error.value = null
      
      try {
        const data = await articleStore.fetchArticleById(id)
        article.value = data
      } catch (err) {
        error.value = err.message || '获取文章详情失败'
        console.error('获取文章详情失败:', err)
      } finally {
        loading.value = false
      }
    }

    // 格式化日期
    const formatDate = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    }

    onMounted(() => {
      fetchArticleDetail()
    })

    return {
      article,
      loading,
      error,
      formatDate
    }
  }
}
</script>

<style scoped>
.article-detail-container {
  width: 100%;
  margin: 0 auto;
  padding: 20px;
}

.loading-state,
.error-state,
.not-found {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  min-height: 400px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.loading-state p {
  margin-top: 20px;
  color: #666;
}

.article-content {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  padding: 30px;
}

.article-header {
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 30px;
}

.article-title {
  font-size: 2.2rem;
  color: #333;
  margin-bottom: 15px;
  line-height: 1.3;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  color: #999;
  font-size: 0.9rem;
}

.article-body {
  line-height: 1.8;
  font-size: 1.1rem;
  color: #333;
  margin-bottom: 30px;
}

/* 文章内容样式 */
.article-body :deep(h1),
.article-body :deep(h2),
.article-body :deep(h3),
.article-body :deep(h4),
.article-body :deep(h5),
.article-body :deep(h6) {
  margin-top: 30px;
  margin-bottom: 15px;
  color: #333;
}

.article-body :deep(h1) { font-size: 1.8rem; }
.article-body :deep(h2) { font-size: 1.6rem; }
.article-body :deep(h3) { font-size: 1.4rem; }

.article-body :deep(p) {
  margin-bottom: 20px;
}

.article-body :deep(img) {
  max-width: 100%;
  height: auto;
  margin: 20px 0;
  border-radius: 4px;
  display: block;
}

.article-body :deep(blockquote) {
  border-left: 4px solid #667eea;
  padding-left: 20px;
  margin: 20px 0;
  color: #666;
  font-style: italic;
}

.article-body :deep(pre) {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 4px;
  overflow-x: auto;
  margin: 20px 0;
}

.article-body :deep(code) {
  background-color: #f5f5f5;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 0.9em;
}

.article-body :deep(pre code) {
  background-color: transparent;
  padding: 0;
}

.article-body :deep(ul),
.article-body :deep(ol) {
  margin: 20px 0;
  padding-left: 30px;
}

.article-body :deep(li) {
  margin-bottom: 8px;
}

.article-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 30px 0 0;
  border-top: 1px solid #f0f0f0;
  background-color: #fafafa;
}

.back-link,
.edit-link {
  text-decoration: none;
}

.back-home {
  margin-top: 20px;
  display: inline-block;
}

/* 侧边栏样式 */
.sidebar {
  padding-top: 40px;
}

.sidebar-card {
  background-color: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 20px;
}

.sidebar-card h3 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
  color: #2c3e50;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 0.9rem;
  border-bottom: 1px dotted #eee;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item .label {
  color: #666;
}

.info-item .value {
  color: #333;
  font-weight: 500;
  text-align: right;
  flex: 1;
  margin-left: 12px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .article-detail-container {
    padding: 15px;
  }
  
  .article-content {
    padding: 35px;
  }
}

@media (max-width: 992px) {
  /* 在中等屏幕上调整边距和间距 */
  .article-content {
    padding: 30px 25px;
  }
  
  .sidebar {
    margin-top: 20px;
    padding-top: 0;
  }
  
  .sidebar-card {
    position: relative;
    top: 0;
  }
}

@media (max-width: 768px) {
  .article-content {
    padding: 25px;
  }
  
  .article-title {
    font-size: 1.8rem;
  }
  
  .article-header {
    margin-bottom: 25px;
    padding-bottom: 15px;
  }
  
  .article-meta {
    gap: 15px;
    font-size: 0.85rem;
    flex-direction: column;
  }
  
  .article-body {
    font-size: 1rem;
    line-height: 1.7;
  }
  
  .sidebar-card {
    padding: 20px;
  }
  
  .info-item {
    flex-direction: column;
    gap: 4px;
  }
  
  .info-item .value {
    text-align: left;
    margin-left: 0;
  }
}

@media (max-width: 480px) {
  .article-detail-container {
    padding: 10px;
  }
  
  .article-content {
    padding: 20px 15px;
    border-radius: 6px;
  }
  
  .article-title {
    font-size: 1.6rem;
    margin-bottom: 12px;
  }
  
  .article-header {
    margin-bottom: 20px;
  }
  
  .article-meta {
    gap: 10px;
    font-size: 0.8rem;
  }
  
  .article-body {
    font-size: 0.95rem;
  }
  
  .article-actions {
    justify-content: center;
    padding: 20px 0 0;
    flex-direction: column;
  }
  
  .article-actions .back-link,
  .article-actions .edit-link {
    width: 100%;
  }
  
  .article-actions .el-button {
    width: 100%;
  }
  
  .loading-state,
  .not-found {
    padding: 60px 15px;
  }
  
  .sidebar-card h3 {
    font-size: 1.1rem;
  }
}
</style>